package com.max.mil.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.max.mil.common.util.DataPage;
import com.max.mil.common.util.HttpTool;
import com.max.mil.common.util.HttpTool.Response;
import com.max.mil.common.util.StringUtil;
import com.max.mil.model.Category;
import com.max.mil.model.FullMinus;
import com.max.mil.model.Product;
import com.max.mil.service.app.YHZService;
import com.max.mil.service.app.impl.YHZServiceImpl;

@Controller
@RequestMapping("/yhz")
public class YHZController extends BaseController
{
	
	YHZService yhzService = new YHZServiceImpl();
	
	
	@RequestMapping("/index")
	public String index() throws Exception 
	{
		return "yhz/blank";
	}
	
	@RequestMapping("/fullCutList")
	public String fullCutList(Integer status, Integer pageNo, Integer pageSize, Model model) throws Exception 
	{
		DataPage<FullMinus> dp = yhzService.getFullMinus(status!=null?status:2, pageNo!=null?pageNo:1, pageSize!=null?pageSize:10);
		model.addAttribute("dp", dp);
		
		return "yhz/fullCutList";
	}
	
	
	@RequestMapping("/fullCutCancel")
	public String fullCutCancel(String id, Model model) throws Exception 
	{
		yhzService.delFullMinus(id);
		
		return "redirect:fullCutList.do?status=0";
	}
	
	
	@RequestMapping("/fullCutTemplet")
	public String fullCutTemplet(String id, Model model) throws Exception 
	{
		FullMinus fullMinus = null;
		if(!StringUtil.isEmpty(id))
			fullMinus = yhzService.getFullMinusDetail(
					StringUtil.convertInt(id.split("_")[0], -1), 
					StringUtil.convertInt(id.split("_")[1], -1));
		
		model.addAttribute("fullMinus", fullMinus);
		return "yhz/fullCutTemplet";
	}
	
	
	@RequestMapping("/fullCutTempletEdit")
	public String fullCutTempletEdit(String id, String title, String date_range, int fm_type, 
			Integer repeat, Integer jl_type, String aaa, String bbb, Model model) throws Exception 
	{
		DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		
		id = yhzService.fullCutTempletReplace(id, title, dateFormat.parse(date_range.split("-")[0].trim()), 
				dateFormat.parse(date_range.split("-")[1].trim()), fm_type, repeat, jl_type, aaa, bbb);
		
		return "redirect:fullCutDetail.do?id="+id;
	}
	
	
	@RequestMapping("/fullCutDetail")
	public String fullCutDetail(String id, Integer pageNo, Integer pageSize, Model model) throws Exception 
	{
		Object obj = YHZServiceImpl.cache.get(id);
		if(obj == null)
		{
			obj = yhzService.getFullMinusDetail(
					StringUtil.convertInt(id.split("_")[0], -1), 
					StringUtil.convertInt(id.split("_")[1], -1));
		}
		model.addAttribute("fullMinus", obj);
		
		return "yhz/fullCutDetail";
	}
	
	
	@ResponseBody
	@RequestMapping("/fullCutDetailEdit")
	public JsonRsp fullCutDetailEdit(String id, String changeInfos) throws Exception 
	{
		JsonRsp jsonRsp = JsonRsp.ERROR;
		
		try
		{
			id = yhzService.fullCutDetailEdit(id, changeInfos);
			jsonRsp = JsonRsp.SUCCESS;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			jsonRsp = new JsonRsp(1,e.getMessage());
		}
		
		return jsonRsp;
	}
	
	
	/**==================================产品相关==========================================*/
	@ResponseBody
	@RequestMapping("/getCateList")
	public JsonRsp getCateList(Integer type) throws Exception 
	{
		JsonRsp jsonRsp = new JsonRsp();
		
		List<Category> list = yhzService.getCateList(type!=null?type:0);
		jsonRsp.setData(list);
		
		return jsonRsp;
	}
	
	
	@ResponseBody
	@RequestMapping("/getProdList")
	public JsonRsp getProdList(String prodName, String prodIds, String prodCode, 
			Integer cateP, Integer cateM, Integer pageNo, Integer pageSize) throws Exception 
	{
		JsonRsp jsonRsp = new JsonRsp();
		
		DataPage<Product> dp = yhzService.getProduct(prodName, prodIds, prodCode,
				cateP, cateM, pageNo!=null?pageNo:1, pageSize!=null?pageSize:10);
		jsonRsp.setData(dp);
		
		return jsonRsp;
	}
	
	/**==================================OAuth2.0==========================================*/
	@RequestMapping("/authorize")
	public void authorize(HttpServletRequest req, HttpServletResponse rsp) throws Exception 
	{
		String client_id = "10420013082700000187";
		String response_type = "code";
		String redirect_uri = "http://app.com:8080/yhz/token.do";
		String scope = "";
		String state = "";
		String view = "";
		
		StringBuffer sb = new StringBuffer();
		sb.append("https://member.1mall.com/login/authorize.do")
		  .append("?client_id=").append(client_id)
		  .append("&response_type=").append(response_type)
		  .append("&redirect_uri=").append(redirect_uri)
		  .append("&scope=").append(scope)
		  .append("&state=").append(state)
		  .append("&view=").append(view);
		System.out.println(sb.toString());
		
		rsp.sendRedirect(sb.toString());
	}
	
	
	@RequestMapping("/token")
	public String accessToken(String code, Model model) throws Exception 
	{
		String client_id = "10420013082700000187";
		String client_secret = "e772e2b27c5446eecc1abf9850de0004";
		String grant_type = "authorization_code";
		String redirect_uri = "http://app.com:8080/yhz/token.do";
		String scope = "";
		String state = "";
		String view = "";
		
		StringBuffer sb = new StringBuffer();
		sb.append("https://member.1mall.com/login/token.do")
		  .append("?client_id=").append(client_id)
		  .append("&client_secret=").append(client_secret)
		  .append("&grant_type=").append(grant_type)
		  .append("&code=").append(code)
		  .append("&redirect_uri=").append(redirect_uri)
		  .append("&scope=").append(scope)
		  .append("&state=").append(state)
		  .append("&view=").append(view);
		System.out.println(sb.toString());
		
		HttpTool httpTool = new HttpTool();
		Response response = httpTool.request(sb.toString());
		JSONObject json = response.asJSONObject();
		if(json != null && json.getString("accessToken") != null)
			model.addAttribute("accessToken", json.getString("accessToken"));
		
		return "yhz/blank";
	}
}

class JsonRsp
{
	int ret = 0;
	String msg = "success!";
	Object data = null;
	
	final static JsonRsp SUCCESS = new JsonRsp(0,"success!");
	final static JsonRsp ERROR   = new JsonRsp(0,"error!");
	
	public JsonRsp()
	{
	}
	
	public JsonRsp(int ret, String msg)
	{
		this.ret = ret;
		this.msg = msg;
	}
	
	public int getRet()
	{
		return ret;
	}
	public void setRet(int ret)
	{
		this.ret = ret;
	}
	public String getMsg()
	{
		return msg;
	}
	public void setMsg(String msg)
	{
		this.msg = msg;
	}

	public Object getData()
	{
		return data;
	}

	public void setData(Object data)
	{
		this.data = data;
	}
}
